<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/28
 * Time: 19:12
 */

namespace app\admin\controller;
use app\admin\model\CourseClass as CourseClassModel;
use app\admin\model\CourseClass;
use think\Db;

class CourseClassController extends BaseController
{
    protected $beforeActionList = [
        'delsoncate'  =>  ['only'=>'del'],
    ];
    public function lst()
    {
        $model = new CourseClassModel();
        $res = $model->cateTree();
        $this->assign('lst',$res);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()){
            $param = $this->request->param();
            $model = new CourseClassModel();
            $res = $model -> allowField(true) -> save($param);
            if ($res){
                $this->success('添加成功',url('courseClass/lst'),'',2);
            }
            $this->error('添加失败');
        }
        $model = new CourseClassModel();
        $res = $model -> cateTree();
        $this->assign('info',$res);
        return $this->fetch();
    }

    public function edit()
    {
        $model = new CourseClassModel();
        if ($this->request->isPost()){
            $post = $this->request->post();
            $res=$model->allowField(true)->save($post,['id'=>input('id')]);
            if($res !==false){
                $this->success('修改成功！',url('courseClass/lst'),'',1);
            }else{
                $this->error('修改失败!');
            }
        }
        $res = CourseClassModel::get(input('id'));
        $info = $model -> cateTree();
        $this->assign([
           'info' => $info,
           'res' => $res,
        ]);
        return $this->fetch();
    }

    public function del()
    {
        $model = new CourseClassModel();
        $res = $model->delete(input('id'));
        if ($res){
            $this->success('删除成功！',url('courseClass/lst'),'',1);
        }else{
            $this->error('删除失败!');
        }
    }

    public function delsoncate(){
        $cateid = input('id/d');
        $model = new CourseClassModel();
        $arr=$model->getchilrenid($cateid);
         $arr[] = $cateid;
        if($arr){
            $res = Db::table('course_class')->delete($arr);
            if ($res){
                $this->success('删除成功！',url('courseClass/lst'),'',1);
            }else{
                $this->error('删除失败!');
            }
        }
    }
}